package net.ibizsys.eclipse.studio.xml;

import java.io.File;

import net.ibizsys.eclipse.studio.util.StringKit;

import org.w3c.dom.Node;

/**
 * 文件
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizFile2 extends IBizSearchableNode
{
	public static final String NODE_IBIZFILE = "IBIZFILE";

	/**
	 * 类型
	 */
	public static final String ATTR_TYPE = "TYPE";

	/**
	 * FOLDER
	 */
	public static final String ATTR_FOLDER = "FOLDER";

	/**
	 * PUB
	 */
	public static final String ATTR_PUB = "PUB";

	/**
	 * USR
	 */
	public static final String ATTR_USR = "USR";

	private IBizFileType type;
	private String pub;
	private String usr;
	private String root;
	private String folder;

	private String fileName;
	private String extension;

	private boolean btwoFile;

	@Override
	public void init(Node node, IBizProject project)
	{
		super.init(node, project);

		if (project != null)
		{
			if (getType().value() > IBizFileType.SF.value() || StringKit.IsEqual(project.getType(), IBizProject.TYPE_SRV, true))
			{
				this.root = project.getJavaRootPath();
			} else if (getType().value() > IBizFileType.PF.value())
			{
				this.root = project.getWebRootPath() + File.separator + project.getCodeName();
			}
		}
		btwoFile = !StringKit.IsBlank(pub) && !StringKit.IsBlank(usr);

		// 截取文件名
		if (!StringKit.IsBlank(pub))
		{
			int nPos = pub.lastIndexOf("/");
			if (nPos != -1)
			{
				String name = pub.substring(nPos + 1);

				this.initAttribute(ATTR_NAME, name);
			} else
			{
				this.initAttribute(ATTR_NAME, pub);
			}
		}

		int nExPos = getName().indexOf(".");
		if (nExPos != -1)
		{
			this.fileName = getName().substring(0, nExPos);
			this.extension = getName().substring(nExPos + 1);
		} else
		{
			this.fileName = getName();
		}
	}

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_TYPE, name, true))
		{
			this.type = IBizFileType.convert(value);
			return;
		}
		if (StringKit.IsEqual(ATTR_PUB, name, true))
		{
			this.pub = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_USR, name, true))
		{
			this.usr = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_FOLDER, name, true))
		{
			this.folder = value;
			return;
		}
		super.initAttribute(name, value);
	}

	public IBizFileType getType()
	{
		return type;
	}

	public String getPub()
	{
		return pub;
	}

	public String getUsr()
	{
		return usr;
	}

	public String getRoot()
	{
		return root;
	}

	public String getFolder()
	{
		return folder;
	}

	public String getFileName()
	{
		return fileName;
	}

	public String getExtension()
	{
		return extension;
	}

	/**
	 * 获取文件绝对路径
	 * 
	 * @param bPubFile 是否PUB文件
	 * @return
	 */
	public String getFileAbsPath(boolean bPubFile)
	{
		if (StringKit.IsBlank(getFolder()))
		{
			return String.format("%1$s/%2$s", getRoot(), bPubFile ? getPub() : getUsr());
		} else
		{
			return String.format("%1$s/%2$s/%3$s", getRoot(), getFolder(), bPubFile ? getPub() : getUsr());
		}
	}

	/**
	 * 有2个文件
	 * 
	 * @return
	 */
	public boolean hasTwo()
	{
		return btwoFile;
	}
}
